package com.viish.apps.tripletriad;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.viish.apps.tripletriad.carte.Card;
import com.viish.apps.tripletriad.carte.DatabaseStream;
import com.viish.apps.tripletriad.views.MinimalistCardView;

public class MyCards extends Activity implements OnClickListener
{
	private ArrayList<Card> currentDeck;
	private ArrayList<MinimalistCardView> currentSelectedViews;
	
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cards);
        
        currentDeck = new ArrayList<Card>();
        currentSelectedViews = new ArrayList<MinimalistCardView>();
        
        DatabaseStream dbs = new DatabaseStream(this);
        ArrayList<Card> cards = dbs.getMyCards();
        dbs.close();
        
        LinearLayout cardsLL = (LinearLayout) findViewById(R.id.cardsLL);
        for (int i = 1; i <= 10; i++)
        {
        	TextView tvLevel = new TextView(this);
        	tvLevel.setTextColor(Color.BLACK);
        	tvLevel.setText(getString(R.string.level) + " " + i);
        	
        	ArrayList<Card> sortByLevel = new ArrayList<Card>();
			for (Card c : cards)
			{
				if (c.getLevel() == i)
				{
					sortByLevel.add(c);
				}
			}
			if (sortByLevel.size() > 0) cardsLL.addView(tvLevel);
			
        	LinearLayout lvlLL = new LinearLayout(this);
        	lvlLL.setOrientation(LinearLayout.HORIZONTAL);
        	lvlLL.setGravity(Gravity.CENTER);
        	lvlLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        	for (Card card : sortByLevel)
        	{
        		Log.d("ERROR", card.toString());
        		MinimalistCardView cardView = new MinimalistCardView(this, card.getView().getBitmap(), card, true);
        		cardView.setTag(card);
        		cardView.setChecked(false);
        		cardView.setOnClickListener(this);
        		lvlLL.addView(cardView);
        		cardView.invalidate();
        	}
        	cardsLL.addView(lvlLL);
        }
        cardsLL.invalidate();
    }
	
	private void createDeckWithSelectedCards(String previousEntry)
	{
		if (currentDeck.size() == 5)
		{
			final Context context = this;
			AlertDialog.Builder alert = new AlertDialog.Builder(this);  

			alert.setTitle(getString(R.string.enter_deck_name));
			alert.setIcon(R.drawable.pampa);
			 
			final EditText deckNameET = new EditText(this);
			if (previousEntry.equals("")) deckNameET.setHint(getString(R.string.deck_name));
			else deckNameET.setText(previousEntry);
			alert.setView(deckNameET);
			
			alert.setNegativeButton(getString(R.string.button_back), null);
			alert.setNeutralButton(getString(R.string.button_create), new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface arg0, int arg1) 
				{
					String deckName = deckNameET.getText().toString();
					if (!deckName.equals(""))
					{
						DatabaseStream dbs = new DatabaseStream(context);
						if (!dbs.isDeckNameAlreadyUsed(deckName))
						{
							if (dbs.createDeck(deckName, currentDeck))
							{
								Toast.makeText(context, getString(R.string.deck_successfully_created), Toast.LENGTH_LONG).show();
								for (MinimalistCardView cv : currentSelectedViews)
								{
									cv.setChecked(false);
								}
								currentSelectedViews = new ArrayList<MinimalistCardView>();
								currentDeck = new ArrayList<Card>();
							}
							else
							{
								Toast.makeText(context, getString(R.string.error_creating_deck), Toast.LENGTH_LONG).show();
							}
						}
						else
						{
							Toast.makeText(context, getString(R.string.deck_name_already_used), Toast.LENGTH_LONG).show();
							createDeckWithSelectedCards(deckName);
						}
						dbs.close();
					}
				}
			});
			
			alert.show();
		}
		else
		{
			Toast.makeText(this, getString(R.string.five_cards_deck), Toast.LENGTH_LONG).show();
		}
	}
	
	public void onClick(View v) 
	{
		MinimalistCardView cardView = (MinimalistCardView) v;
		cardView.swapChecked();
		
		Card card = (Card) v.getTag();
		if (cardView.isChecked()) 
		{
			currentDeck.add(card);
			currentSelectedViews.add((MinimalistCardView) v);
		}
		else 
		{
			currentDeck.remove(card);
			currentSelectedViews.remove(v);
		}
		
		if (currentDeck.size() == 5)
		{
			openOptionsMenu();
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) 
    {
    	super.onCreateOptionsMenu(menu);
    	
    	MenuItem menuItem;
    	menuItem = menu.add(0, 0, 0, getString(R.string.menu_create_deck));
    	menuItem.setIcon(android.R.drawable.ic_menu_manage);
    	
    	return true;
    }
    
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
    {
    	super.onMenuItemSelected(featureId, item);
    	
    	switch (item.getItemId())
    	{
    		case 0:
    			createDeckWithSelectedCards("");
    			break;
    	}
    	
    	return false;
    }
}
